package com.bw.skill.mvvm.view;


import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.bw.mvvm_common.mvvm.view.BaseActivity;
import com.bw.skill.BR;
import com.bw.skill.R;
import com.bw.skill.TimeService;
import com.bw.skill.action.SkillAction;
import com.bw.skill.databinding.ActivitySkillListBinding;
import com.bw.skill.mvvm.viewmodel.SecKillViewModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class SkillListActivity extends BaseActivity<ActivitySkillListBinding, SecKillViewModel> {

    boolean serviceFlag = false;
    private TimeService timeService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (serviceFlag) {
            Intent intent = new Intent(this, TimeService.class);
            bindService(intent, connection, BIND_AUTO_CREATE);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (serviceFlag) {
            unbindService(connection);
        }
    }

    @Override
    public int bindLayout() {
        BarUtils.transparentStatusBar(this);
        BarUtils.setStatusBarLightMode(this,true);
        return R.layout.activity_skill_list;
    }

    @Override
    public int initVerable() {
        return com.bw.skill.BR.vm;
    }

    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            TimeService.TimeBinder binder = (TimeService.TimeBinder) service;
            timeService = binder.getTimeService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

//1635334984
//1635334984

    //1635379746
    //1635379774

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void skill(SkillAction action){
          if (action.action.equals("SkillListActivity"))  {
              serviceFlag = true;
              Intent intent = new Intent(this, TimeService.class);
              intent.putExtra("time",action.nowTime);
              startService(intent);
              Intent bindIntent = new Intent(this, TimeService.class);
              bindService(bindIntent, connection, BIND_AUTO_CREATE);
          }else if (action.action.equals("SkillListGetNowTime")){
              long nowTime = timeService.getTime();
              LogUtils.e("bb",nowTime);
              vm.requestSkill(nowTime);
          }
    }

}